//
//  CCHatRecordCell.m
//  wanzhaoIM
//
//  Created by wanzhao on 16/1/7.
//  Copyright © 2016年 liuyuantao. All rights reserved.
//

#import "CCHatRecordCell.h"
#import "UIUtil.h"
#import "CXmppRequestMgr.h"
#import "UIImageView+WebCache.h"

@interface CCHatRecordCell ()

{
    UIImageView *_userHead;
    UIImageView *_bageView;
    UILabel *_userNickname;
    UILabel *_messageConent;
    UILabel *_timeLable;
    UIView *_line;
    UIImageView *_arrow;
}
@end
@implementation CCHatRecordCell

+ (instancetype)chatRecordCellWithTableView:(UITableView *)tableView
{
    static NSString *chatRecordId = @"chatRecordCellId";
    CCHatRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:chatRecordId];
    if (cell == nil) {
        cell = [[CCHatRecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:chatRecordId];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleGray;
        
        _arrow = [[UIImageView alloc] init];
        _arrow.image = [UIImage imageNamed:@"aaaa"];
        _arrow.userInteractionEnabled = YES;
        
        _userHead =[[UIImageView alloc] initWithFrame:CGRectZero];
        _userHead.userInteractionEnabled = YES;
        [_userHead.layer setCornerRadius:3.0f];
        [_userHead.layer setMasksToBounds:YES];
        
        _userNickname=[[UILabel alloc]initWithFrame:CGRectZero];
        [_userNickname setFont:nameFont];
        [_userNickname setTextColor:[UIUtil colorWithHexString:@"#595757" withAlpha:1]];
        [_userNickname setBackgroundColor:ClearColor];

        _messageConent=[[UILabel alloc]initWithFrame:CGRectZero];
        _messageConent.numberOfLines = 1;
        [_messageConent setFont:contentFont];
        [_messageConent setTextColor:[UIColor lightGrayColor]];
        [_messageConent setBackgroundColor:[UIColor clearColor]];
        
        
        _timeLable=[[UILabel alloc]initWithFrame:CGRectZero];
        [_timeLable setTextColor:[UIColor lightGrayColor]];
        [_timeLable setFont:timeFont];
        [_timeLable setTextAlignment:NSTextAlignmentRight];
        [_timeLable setBackgroundColor:[UIColor clearColor]];
        
        _line = [[UIView alloc] init];
        _line.backgroundColor = [UIColor lightGrayColor];
        
        [self.contentView addSubview:_userHead];
        [self.contentView addSubview:_userNickname];
        [self.contentView addSubview:_messageConent];
        [self.contentView addSubview:_timeLable];
        [self.contentView addSubview:_line];
        [self.contentView addSubview:_arrow];
        
        self.backgroundColor = [UIColor whiteColor];//[UIUtil colorWithHexString:@"#f9f5eb" withAlpha:1.0];
        
        //        self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
        //        self.selectedBackgroundView.backgroundColor = [UIColor blueColor];
    }
    return self;
}


- (void)setMessageFrame:(CMessageFrame *)messageFrame
{
    _messageFrame = messageFrame;
    ChatMessageListCellData *message = messageFrame.message;
    
    [_userHead sd_setImageWithURL:[NSURL URLWithString:message.headImageUri] placeholderImage:[UIImage imageNamed:@"110-110"]];
    _userHead.frame = messageFrame.iconFrame;
    
    _userNickname.text = message.senderName;
    _userNickname.frame = messageFrame.nameFrame;
    
    _timeLable.text = message.sendTime;
    _timeLable.frame = messageFrame.timeFrame;
    
    _messageConent.text = message.content;
    _messageConent.frame = messageFrame.contentFrame;
    
    _line.frame = messageFrame.lineframe;
    _arrow.frame = messageFrame.arrowFrame;
}
- (void)setMessageFrame:(CMessageFrame *)messageFrame withCount:(NSInteger)count
{
    _messageFrame = messageFrame;
    ChatMessageListCellData *message = messageFrame.message;
    
    [_userHead sd_setImageWithURL:[NSURL URLWithString:message.headImageUri] placeholderImage:[UIImage imageNamed:@"110-110"]];
    _userHead.frame = messageFrame.iconFrame;
    
    _userNickname.text = message.senderName;
    _userNickname.frame = messageFrame.nameFrame;
    
    _timeLable.text = message.sendTime;
    _timeLable.frame = messageFrame.timeFrame;
    _timeLable.hidden = YES;
    
    _messageConent.text = [NSString stringWithFormat:@"有%lu条相关记录",count];
    //    _messageConent.frame = messageFrame.contentFrame;
    _messageConent.frame = CGRectMake(messageFrame.contentFrame.origin.x, messageFrame.contentFrame.origin.y, self.frame.size.width, messageFrame.contentFrame.size.height);
    _line.frame = messageFrame.lineframe;
    _arrow.frame = messageFrame.arrowFrame;
}


//bug 8514 fixed by lixu
-(void)setCMessageFrame:(CMessageFrame *)messageFrame withSearchText:(NSString *)searchText
{
    _messageFrame = messageFrame;
    ChatMessageListCellData *message = messageFrame.message;
    
    [_userHead sd_setImageWithURL:[NSURL URLWithString:message.headImageUri] placeholderImage:[UIImage imageNamed:@"110-110"]];
    _userHead.frame = messageFrame.iconFrame;
    
    _userNickname.text = message.senderName;
    _userNickname.frame = messageFrame.nameFrame;
    
    _timeLable.text = message.sendTime;
    _timeLable.frame = messageFrame.timeFrame;
    NSRange conentRanage = [[message.content lowercaseString] rangeOfString:[searchText lowercaseString]];
    NSString *str;
    if (conentRanage.length>0) {
        if (conentRanage.location != 0 && message.content.length >18) {
            str = [message.content substringFromIndex:conentRanage.location];
            str = [@"..."stringByAppendingString:str];
        }
        else
        {
            str = message.content;      
        }
        NSArray *rangeArray = [[self getRangeStr:str findText:searchText] copy];
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:str];
        for (NSInteger i = 0; i<rangeArray.count; i++) {
            NSRange okrange = [rangeArray[i] rangeValue];
            [attStr addAttribute:NSForegroundColorAttributeName value:RGBCOLORWITH16COLOR(0Xfb8d44) range:okrange];
        }
        _messageConent.attributedText = attStr;
    }
    else{
    _messageConent.text = message.content;
    }
    _messageConent.frame = messageFrame.contentFrame;
    _line.frame = messageFrame.lineframe;
    _arrow.frame = messageFrame.arrowFrame;

}
-(NSMutableArray *)getRangeStr:(NSString *)text findText:(NSString *)findText
{
    NSMutableArray *arrayRanges = [NSMutableArray arrayWithCapacity:10];
    if (findText == nil || [findText isEqualToString:@""]) {
        return nil;
    }
    //bug 9532 fixed by lixu
    NSRange rang = [[text lowercaseString] rangeOfString:[findText lowercaseString]];
    if (rang.location != NSNotFound && rang.length != 0) {
        [arrayRanges addObject:[NSValue valueWithRange:rang]];
        NSRange rang1 = {rang.location,rang.length};
        NSInteger location = 0;
        NSInteger length = 0;
        for (NSInteger i = 0;; i++)
        {
                location = rang1.location + rang1.length;
                length = text.length - rang1.location - rang1.length;
                rang1 = NSMakeRange(location, length);
            rang1 = [text rangeOfString:findText options:NSCaseInsensitiveSearch range:rang1];
            if (rang1.location == NSNotFound || rang1.length == 0) {
                break;
            }else
                 [arrayRanges addObject:[NSValue valueWithRange:rang1]];
        }
        return arrayRanges;
    }
    return nil;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
